// @flow
import React, { Component } from 'react'
import styles from './Fault.css'
import { Form, Button } from 'antd'
import FaultGrid1 from './FaultGrid1'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
import { connect } from 'react-redux'
import moment from 'moment'
import ReportFilter from 'components/ReportFilter'

type Props = {
  history: Object,
  form: Object,
  userId: string
}
type States = {
  deviceData: Array<any>,
  rowData: Array<Object>,
  total: number,
  current: number,
  pageSize: number
}
class Fault1 extends Component {
  props: Props
  state: States
  queryUrl: String
  getFaults: Function
  addFault: Function
  handleSearch: Function
  getDeviceData: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      deviceData: [],
      rowData: [],
      total: 0,
      current: 1,
      pageSize: 20
    }
    this.handleSearch = this.handleSearch.bind(this)
    this.getFaults = this.getFaults.bind(this)
    this.addFault = this.addFault.bind(this)
  }
  handleSearch (searchQuery) {
    this.queryUrl = searchQuery
    this.getFaults(1, 20)
  }
  componentWillMount () {
    this.handleSearch('')
  }

  getFaults (page: number, pageSize: number) {
    let queryUrl = this.queryUrl || ``
    const { userId } = this.props
    universalFetch(`${__SERVICE_API__}device/report?userId=${userId}&pageNum=${page}&pageSize=${pageSize}&order=report_time${queryUrl}`)
      .then(res => res.json())
      .then((json) => {
        if (json.code !== 200000) {
          throw new Error(JSON.stringify({ code: json.code, message: json.message }))
        }
        this.setState({
          current: page,
          total: json.pageInfo.total,
          rowData: json.result.map((d, i) => {
            d['reportTime'] = moment(d.reportTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
            return d
          })
        })
      })
      .catch((error) => {
        handleFetchError(error)
        console.error(error)
      })
  }

  addFault () {
    this.props.history.push('/business/fault-add1')
  }
  render () {
    const { rowData, current, pageSize, total } = this.state
    const { history } = this.props
    return (
      <div className={styles['main']}>
        <div className={styles['fillterMenu']}>
          <div className={styles['mainBar']}>
            <Button type='primary' className={styles['addBtn']}
              onClick={() => history.push('/business/report-add')}>添加分析报告</Button>
          </div>
          <ReportFilter getData={this.handleSearch} />
        </div>
        <FaultGrid1 {...{
          current,
          pageSize,
          total,
          refreshFaults: this.getFaults,
          rowData: rowData,
          history: this.props.history
        }} />
      </div>
    )
  }
}

const mapStateToProps = state => {
  return {
    userId: state.user.userId,
    userMenu: state.menu.userMenu
  }
}
export default connect(mapStateToProps, {})(Form.create()(Fault1))
